﻿using System;
using System.Collections.Generic;
using System.Text;
using Common;
using System.Data;
using System.Data.SqlClient;
using Entities;

namespace Dao
{
    /// <summary>
    /// Provides a way of query with Function from a SQL Server Database
    /// </summary>
    public class FunctionData
    {
        #region Variables private
        IList<Function> _listFunc = null;
        Function _func = null; 
        #endregion

        /// <summary>
        /// Display all list Function
        /// </summary>
        /// <returns>List Function</returns>
        public IList<Function> GetAll()
        {
            SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
            _listFunc = new List<Function>();
            try
            {
                SqlDataReader reader = SQLDBHelper.ExecuteReader(conn, CommandType.StoredProcedure, "sp_Function_GetAll", null);
                while (reader.Read())
                {
                    _func = new Function();
                    if (!reader.IsDBNull(0))
                        _func.FunctionID = reader.GetByte(0);
                    if (!reader.IsDBNull(1))
                        _func.FunctionName = reader.GetString(1);
                    _listFunc.Add(_func);
                }
            }
            catch (Exception ex)
            {
                ErrorManagement.TrackLog(ex.Message);
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return _listFunc;
        }
        /// <summary>
        /// Insert new Function
        /// </summary>
        /// <param name="func">Colection properties of Function</param>
        /// <returns>True is successful , False is unsuccessful</returns>
        public bool InsertOrUpdate(Function func)
        {
            SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
            int result = 0;
            try
            {
                SqlParameter[] parms = new SqlParameter[]{
                                        new SqlParameter("@FunctionID", SqlDbType.TinyInt),
                                        new SqlParameter("@FunctionName", SqlDbType.NVarChar, 150)
                                    };
                parms[0].Value = func.FunctionID;
                parms[1].Value = func.FunctionName;

                if (func.FunctionID == 1)
                    result = SQLDBHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "sp_Function_Insert", parms);
                else
                    result = SQLDBHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "sp_Function_Update", parms);
            }
            catch (Exception ex)
            {
                ErrorManagement.TrackLog(ex.Message);
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return result > 0 ? true : false;
        }
    }
}
